package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.RedisKey;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.search.service.ITravelEsService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("mine")
public class MineController {
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private ITravelStatisRedisService travelStatisRedisService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private ITravelCommentService travelCommentService;

    @RequestMapping("/homepage")
    public String home(Model model, @UserParam UserInfo userInfo) {
        model.addAttribute("userInfo", userInfo);
        //查询最新游记
        List<Travel> travels = travelService.getNewTravel(userInfo.getId());

        //查询用户游记数量
        int count = travelService.getTravelConut(userInfo.getId());
        //遍历用户的所有游记
        for (Travel travel : travels) {
            //从redis里获取获取点赞数
            TravelStatisVO travelVo = travelStatisRedisService.getTravelVo(travel.getId());

            //获取游记目的地的父目的地的对象("上海"---->"中国")
            Long destId = travel.getDest().getId();

            Destination hasParent = destinationService.getParentByName(destId);

            //将对象含有父目的对象的传给travel
            travel.setDest(hasParent);

            if (travelVo != null) {
                //将从redis获取的点赞数设置到travel对象里
                travel.setThumbsupnum(travelVo.getThumbsupnum());
            }
        }
        model.addAttribute("travels", travels);
        return "/mine/homepage";
    }

    /**
     * 个人设置
     *
     * @param model
     * @param userInfo
     * @return
     */
    @RequestMapping("/setting")
    public Object setting(Model model, @UserParam UserInfo userInfo) {
        //共享登录用户
        model.addAttribute("userInfo", userInfo);

        return new AjaxResult();
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) {
        String upload = UploadUtil.uploadHeadImg(pic, filePath);
        AjaxResult result = new AjaxResult();
        result.setData(upload);
        return result;
    }

    /**
     * 修改个人信息
     *
     * @param userInfo 登录用户
     * @return
     */
    @RequestMapping("/input")
    @ResponseBody
    public Object input(UserInfo userInfo) {
        userInfoService.update(userInfo);
        return new AjaxResult();
    }

    /**
     * 我的游记
     *
     * @param model
     * @param qo
     * @param userInfo
     * @return
     */
    @RequestMapping("/mytravelnotes")
    public Object mytravelnotes(Model model, TravelQuery qo, @UserParam UserInfo userInfo) {
        //查询该用户的游记
        List<Travel> travels = travelService.listByUserId(userInfo.getId());
        //总游记数
        int count = travels.size();
        //总回复数
        Long replays = 0L;
        //总阅读数
        Long views = 0L;

        for (Travel travel : travels) {
            views += travel.getViewnum();
            replays += travel.getReplynum();
        }
        //用户信息
        model.addAttribute("userInfo", userInfo);
        //总游记数量
        model.addAttribute("count", count);
        //分页查询我的全部游记
        PageInfo pageInfo = travelService.queryByUserId(qo, userInfo.getId());

        //获取用户的所有游记
        List<Travel> travelList = pageInfo.getList();

        //迭代游记
        for (Travel travel : travelList) {

            //获取游记目的地的父目的地的对象("上海"---->"中国")
            Destination dest = travel.getDest();
            Long destId = dest.getId();
            Destination hasParent = destinationService.getParentByName(destId);

            //将对象含有父目的对象的传给travel
            travel.setDest(hasParent);

            //获取redis的vo数据
            TravelStatisVO travelVo = travelStatisRedisService.getTravelVo(travel.getId());
            if (travelVo != null) {
                //将从redis获取的点赞数设置到travel对象里
                travel.setThumbsupnum(travelVo.getThumbsupnum());
            }
        }
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("replays", replays);
        model.addAttribute("views", views);


        return new AjaxResult();
    }

    /**
     * 用户收藏数据
     *
     * @param userInfo 登录用户
     * @return
     */
    @RequestMapping("/travelcollection")
    public String travelcollection(Model model, @UserParam UserInfo userInfo) {

        List<Travel> travels = new ArrayList<>();

        List<StrategyDetail> strategies = new ArrayList<>();

        List<Destination> destinations = new ArrayList<>();
        //查询地点收藏
        Set<String> destinationsId = userInfoRedisService.getDestById(userInfo.getId());
        //查询redis的游记收藏
        List<String> travelsId = userInfoRedisService.getTravelcollectionById(userInfo.getId());
        
        //查询redis的攻略收藏
        List<Integer> strategiesId = userInfoRedisService.getStrategycollectionById(userInfo.getId());

        if (strategiesId != null) {
            for (Integer aLong : strategiesId) {
                long longValue = aLong.longValue();
                StrategyDetail strategy = strategyDetailService.get(longValue);
                strategies.add(strategy);
            }

        }
        if (travelsId != null) {
            for (String tid : travelsId) {
                Long aLong = Long.valueOf(tid);
                Travel travel = travelService.get(aLong);
                travels.add(travel);
            }

        }
//        if (destinationsId != null) {
//            for (String did : destinationsId) {
//                Long aLong = Long.valueOf(did);
//                Destination dest = destinationService.get(aLong);
//                destinations.add(dest);
//            }
//        }


        model.addAttribute("travels", travels);
        model.addAttribute("strategies", strategies);
        model.addAttribute("destinations", destinations);

        return "/mine/travelcollection";
    }

    @RequestMapping("/review")
    public Object review(Model model, @UserParam UserInfo userInfo) {

        //用户攻略评论
        List<StrategyComment> strategyComments = strategyCommentService.queryByUserId(userInfo.getId());

        //用户游记评论
        List<TravelComment> travelComments = travelCommentService.selectByUserId(userInfo.getId());
        model.addAttribute("strategyComments", strategyComments);
        model.addAttribute("travelComments", travelComments);
//        //点赞数
//        Long thumbnum = 0L;
//        for (StrategyComment strategyComment : strategyComments) {
//            thumbnum += strategyComment.getThumbupnum();
//        }
//        for (TravelComment travelComment : travelComments) {
//            thumbnum += travelComment.getThumbnum();
//        }
//        model.addAttribute("thumbnum", thumbnum);
        return new AjaxResult();
    }
}
